package com.ltu.timkor6.gamesite.users.client;

import com.claudiushauptmann.gwt.recaptcha.client.Recaptcha;
import com.claudiushauptmann.gwt.recaptcha.client.RecaptchaWidget;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.ltu.timkor6.gamesite.shared.CompositeAbstract;

public class RegisterComposite extends CompositeAbstract
{
	private VerticalPanel errors;
	private TextBox textUsername;
	private PasswordTextBox textPassword;
	private PasswordTextBox textRePassword;
	private TextBox textEmail;
	private CheckBox checkTestUser;
	private RecaptchaWidget captcha;
	
	public RegisterComposite(final Register register)
	{
		// Title
		final Label title = new Label("Register");
		title.setStyleName("panelHeader");
		panel.add(title);
		
		// Errors
		errors = new VerticalPanel();
		errors.setStyleName("errors");
		panel.add(errors);
		
		
		// Sign in table
		FlexTable table = new FlexTable();
		
		Label labelUsername = new Label("Username");
		textUsername = new TextBox();
		textUsername.addKeyPressHandler(new KeyPressHandler()
		{
			public void onKeyPress(KeyPressEvent event)
			{
				if(event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER)
				{
					register.checkSubmit();
				}
			}
		});
		
		Label labelPassword = new Label("Password");
		textPassword = new PasswordTextBox();
		textPassword.addKeyPressHandler(new KeyPressHandler()
		{
			public void onKeyPress(KeyPressEvent event)
			{
				if(event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER)
				{
					register.checkSubmit();
				}
			}
		});
		
		Label labelRePassword = new Label("Re-Password");
		textRePassword = new PasswordTextBox();
		textRePassword.addKeyPressHandler(new KeyPressHandler()
		{
			public void onKeyPress(KeyPressEvent event)
			{
				if(event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER)
				{
					register.checkSubmit();
				}
			}
		});
		
		Label labelEmail = new Label("Email");
		textEmail = new TextBox();
		textEmail.addKeyPressHandler(new KeyPressHandler()
		{
			public void onKeyPress(KeyPressEvent event)
			{
				if(event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER)
				{
					register.checkSubmit();
				}
			}
		});
		
		Label labelTestUser = new Label("Test user:");
		checkTestUser = new CheckBox();
		
		Button submit = new Button("Register");
		submit.addClickHandler(new ClickHandler()
		{
			public void onClick(ClickEvent event)
			{
				register.checkSubmit();
			}
		});
		
		//HTML captcha = new HTML("<script type=\"text/javascript\" src=\"http://www.google.com/recaptcha/api/challenge?k=6Lc8W8ASAAAAAIDKyWmL7oE2me3vnBEd0ZoZRY1r\"></script><noscript><iframe src=\"http://www.google.com/recaptcha/api/noscript?k=6Lc8W8ASAAAAAIDKyWmL7oE2me3vnBEd0ZoZRY1r\" height=\"300\" width=\"500\" frameborder=\"0\"></iframe><br><textarea name=\"recaptcha_challenge_field\" rows=\"3\" cols=\"40\"></textarea><input type=\"hidden\" name=\"recaptcha_response_field\" value=\"manual_challenge\"></noscript>");
		captcha = new RecaptchaWidget("6Lc8W8ASAAAAAIDKyWmL7oE2me3vnBEd0ZoZRY1r");
		
		table.setWidget(0, 0, labelUsername);
		table.setWidget(0, 1, textUsername);
		table.setWidget(1, 0, labelPassword);
		table.setWidget(1, 1, textPassword);
		table.setWidget(2, 0, labelRePassword);
		table.setWidget(2, 1, textRePassword);
		table.setWidget(3, 0, labelEmail);
		table.setWidget(3, 1, textEmail);
		table.setWidget(4, 0, labelTestUser);
		table.setWidget(4, 1, checkTestUser);
		table.setWidget(5, 1, captcha);
		table.setWidget(6, 1, submit);
		
		panel.add(table);
		
		textUsername.setFocus(true);
		
		initWidget(panel);
	}
	
	public void addError(String text)
	{
		this.errors.add(new Label(text));
	}
	
	public void clearErrors()
	{
		this.errors.clear();
	}
	
	public String getEmail()
	{
		return this.textEmail.getText().trim();
	}
	
	public String getUsername()
	{
		return this.textUsername.getText().trim();
	}
	
	public String getPassword()
	{
		return this.textPassword.getText().trim();
	}
	
	public String getRePassword()
	{
		return this.textRePassword.getText().trim();
	}
	
	public boolean getTestUser()
	{
		return this.checkTestUser.getValue();
	}
	
	public String getCaptchaChallange()
	{
		return Recaptcha.getChallenge();
	}
	
	public String getCaptchaResponse()
	{
		return Recaptcha.getResponse();
	}
}
